package com.site_assembly.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.site_assembly.config.DeadLetterRabbitConfig;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
public class DeadLetterRabbitController {

    @Resource
    private RabbitTemplate rabbitTemplate;


    @GetMapping("sendDeadLetterTest")
    public String send() throws JsonProcessingException {

        rabbitTemplate.convertAndSend(
                DeadLetterRabbitConfig.DL_FINISH_EXCHANGE,
                DeadLetterRabbitConfig.DL_FINISH_RK,
                "一条信息",
                new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setExpiration(""+(30*1000));
                        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                        return message;
                    }
                });
        return "success";
    }
}